package a06_栈与队列;

import java.util.Stack;

/**
 * <p>
 * a06_逆波兰表达式求值复习2
 * </p>
 *
 * @author flyduck
 * @since 2024-12-29
 */
public class a06_逆波兰表达式求值复习2 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if("+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token)){
                Integer pop1 = stack.pop();
                Integer pop2 = stack.pop();
                if("+".equals(token)){
                    stack.push(pop2 + pop1);
                }else if("-".equals(token)){
                    stack.push(pop2 - pop1);
                }else if("*".equals(token)){
                    stack.push(pop2 * pop1);
                }else if("/".equals(token)){
                    stack.push(pop2 / pop1);
                }
            }else {
                stack.push(Integer.valueOf(token));
            }
        }
        return stack.pop();
    }
}
